﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;
using System.Xml.Linq;


namespace Docentric.Examples.Browser
{
	public class RenderingCollectionXmlExample : ExampleBase
	{
		// Constructor
		public RenderingCollectionXmlExample()
		{
			Name = "RenderingCollectionXml";
			Title = "Rendering Collection (XML)";
			Description = "This time we want to create a report listing all purchase orders in a table. For this task we are using the 'List' element in combination with several 'Field' elements. The default data source is an XML document containing list of products.";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Load the products XML.
			XElement productsXElement = XElement.Load(ApplicationPath + "\\Xml\\Products.xml");

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(productsXElement);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
